package bdz.projects.lavender.opsm.main
{
	
	import flash.events.*;
	import flash.display.*;
	/**
	 * ...
	 * @author ...
	 */
	public class OPSMBackground extends Sprite 	{
		private var bgImage:MovieClip;
		private var bgGradient:MovieClip;
		
		function OPSMBackground() {
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);
		}
		private function initializeComponent(evt:Event):void {
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			createElements();
		}
		private function createElements():void {
			bgImage = new BackgroundImage();
			addChild(bgImage);
			
			bgGradient = new Gradient();
			addChild(bgGradient);
			stage.addEventListener(Event.RESIZE, onStageResize);
			onStageResize();
		}
		private function onStageResize(evt:Event = null):void {
			var maxWidth:Number = Math.ceil((stage.stageHeight/bgImage.height)*bgImage.width);
			var dw:Number=maxWidth;
			var dh:Number=stage.stageHeight;
			var dx:Number=stage.stageWidth/2-maxWidth/2;
			var dy:Number=0;

			if (maxWidth<stage.stageWidth) {
				//try maxing the from the sides
				var maxHeight:Number = Math.ceil((stage.stageWidth/bgImage.width)*bgImage.height);
				var dw2:Number=stage.stageWidth;
				var dh2:Number=maxHeight;
				var dx2:Number=0;
				var dy2:Number=stage.stageHeight/2-maxHeight/2;
				var cutpercent:Number = Math.ceil((stage.stageHeight/maxHeight)*100);
				bgImage.width=dw2;
				bgImage.height=dh2;
				bgImage.x=dx2;
				bgImage.y=dy2;
				
			} else {
				bgImage.x=dx;
				bgImage.y=dy;
				bgImage.width=dw;
				bgImage.height=dh;
			}
			bgGradient.width = stage.stageWidth;
			bgGradient.height = stage.stageHeight;
		}
	}
	
}